package com.snail.readyvr.activity;

import android.view.View;
import android.widget.TextView;

import com.snail.readyvr.R;
import com.snail.readyvr.application.MyApplication;
import com.snail.readyvr.di.component.DaggerChangePswActivityPresenterComponent;
import com.snail.readyvr.di.module.ChangePswActivityPresenterModule;
import com.snail.readyvr.interfaces.MaxLengthWatcherListener;
import com.snail.readyvr.mvp.contract.ChangePswActivityContract;
import com.snail.readyvr.mvp.model.ChangePswParam;
import com.snail.readyvr.mvp.presenter.ChangePswActivityPresenter;
import com.snail.readyvr.rest.RestApiUtils;
import com.snail.readyvr.ui.IndicatorTiTle;
import com.snail.readyvr.ui.ProgressButton;
import com.snail.readyvr.ui.ShowPassWordEditText;
import com.snail.readyvr.util.ApiException;
import com.snail.readyvr.util.Constant;
import com.snail.readyvr.util.MD5Utils;
import com.snail.readyvr.util.MaxLengthWatcher;
import com.snail.readyvr.util.StartActivityUtils;
import com.snail.readyvr.util.TBSnackBarUtils;
import com.snail.readyvr.util.Utils;

import javax.inject.Inject;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by MRKING on 2016/10/18.
 */

public class ChangePswActivity extends CheckPermissionsActivity
        implements MaxLengthWatcherListener,ChangePswActivityContract.View{

    @BindArray(R.array.change_psw_title)
    String[] mTitles;
    @BindView(R.id.change_psw_indicator)
    IndicatorTiTle mChangePswIndicator;
    @BindView(R.id.old_psw)
    ShowPassWordEditText mOldPsw;
    @BindView(R.id.new_psw)
    ShowPassWordEditText mNewPsw;
    @BindView(R.id.change_psw_error_tip)
    TextView mChangePswErrorTip;
    @BindView(R.id.change_psw_btn)
    ProgressButton mChangePswBtn;

    private boolean mOldPswOk = false;
    private boolean mNewPswOk = false;

    @Inject
    ChangePswActivityPresenter mPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_psw;
    }

    @Override
    protected void init() {
        DaggerChangePswActivityPresenterComponent.builder()
                .applicationComponent(((MyApplication)getApplication()).getApplicationComponent())
                .changePswActivityPresenterModule(new ChangePswActivityPresenterModule(this))
                .build().inject(this);
        mChangePswIndicator.setTitls(mTitles);
        mChangePswBtn.setText(R.string.submit);
        mChangePswBtn.setEnabled(false);
        MaxLengthWatcher oldPswWatcher = new MaxLengthWatcher(50, mOldPsw);
        oldPswWatcher.setMaxLengthWatcherListener(this);
        mOldPsw.setTextWatcher(oldPswWatcher);
        mOldPsw.setActivated(false);

        MaxLengthWatcher newPswWatcher = new MaxLengthWatcher(50, mNewPsw);
        newPswWatcher.setMaxLengthWatcherListener(this);
        mNewPsw.setTextWatcher(newPswWatcher);
        mNewPsw.setActivated(false);


    }


    @OnClick({R.id.change_psw_back, R.id.change_psw_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.change_psw_back:
                finish();
                break;
            case R.id.change_psw_btn:
                if(Utils.checkNetWork(this)){
                    changeState(true);
                    ChangePswParam param = new ChangePswParam();
                    param.setType(Constant.REGISTER_PSW_TYPE);
                    param.setPassword(MD5Utils.getMD5_16(mOldPsw.getText().toString().trim()));
                    param.setNewPsw(MD5Utils.getMD5_16(mNewPsw.getText().toString().trim()));
                    param.setUsername(Utils.sUser.getUserName());
                    mPresenter.changePsw(param);
                }
                break;
        }
    }
    public void changeState(boolean loading){
        mOldPsw.setActivated(false);
        mOldPsw.setEnabled(!loading);
        mNewPsw.setActivated(false);
        mNewPsw.setEnabled(!loading);
        mChangePswBtn.setText(loading?R.string.submiting:R.string.submit);
        mChangePswBtn.setShowProgress(loading);
        mChangePswBtn.setEnabled(!loading);
    }


    @Override
    public void overMaxLength(View view, int max) {

    }

    @Override
    public void lengthChanged(View view, int length) {
        switch (view.getId()){
            case R.id.old_psw:
                if(length > 0 && Utils.checkPsw(mOldPsw.getText().toString().trim()))
                    mOldPswOk = true;
                else
                    mOldPswOk = false;
                break;
            case R.id.new_psw:
                if(length > 0 && Utils.checkPsw(mNewPsw.getText().toString().trim()))
                    mNewPswOk = true;
                else
                    mNewPswOk = false;
                break;
        }

        changeBtnState();

    }

    private void changeBtnState() {
        if(mOldPswOk && mNewPswOk && !mChangePswBtn.isEnabled()){
            mChangePswBtn.setEnabled(true);
        }else if (!mOldPswOk || !mNewPswOk && mChangePswBtn.isEnabled()){
            mChangePswBtn.setEnabled(false);
        }
    }

    @Override
    public void changePswSuccess() {
        StartActivityUtils.startMeActivity(this,true);
        finish();
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void dismissProgress() {

    }

    @Override
    public void error(Throwable throwable) {
        changeState(false);
        if (throwable != null) {
            if (throwable instanceof ApiException) {
                ApiException apiException = (ApiException) throwable;
                TBSnackBarUtils.showShort(this, Utils.getErrorTip(apiException.getCode()) + "");
                if(apiException.getWhichApi() == RestApiUtils.CHANGE_PSW_API){//处理一些特殊错误
                    /*if(apiException.getCode() == 3006 || apiException.getCode() == 3007 ){
                        mPhoneNumClearEditText.setActivated(true);
                    }*/
                }
            } else {
                TBSnackBarUtils.showShort(this, throwable.getMessage() + "");
            }
        }else {
            TBSnackBarUtils.showShort(this, R.string.code_default);
        }

    }

    @Override
    protected void hasGetAllPermissions() {

    }
}
